//封装下拉刷新,无限加载

export default ({ getPage, list = "list", page = "page" }) => {
  return {
    data() {
      return { [list]: [], [page]: 1, loadFlag: 0 };
    },
    onPullDownRefresh() {
      if(this.loadFlag) return
      this.Refresh();
    },
    onReachBottom() {
      if(this.loadFlag) return
      this.loadFlag = 1;
      getPage.call(this, this[page], this.__pulldone);
    },
    onLoad() {
      this.Refresh();
    },
    methods: {
      Refresh() {
        this[page] = 1;
        this.loadFlag = 1;
		this.list =[]
	     getPage.call(this, this[page], this.__pulldone);
      },
      __pulldone(data) {
        this.loadFlag = 0;
        var db = data || [];
        if (this[page] == 1) {
          this[list] = db;
        } else {
          this[list] = (this[list] || []).concat(db);
        }
        uni.stopPullDownRefresh();
        this[page]++;
      }
    }
  };
};
